package com.windy.dailynews.presenter.impl;

import com.google.gson.Gson;
import com.windy.dailynews.model.ThemeNews;
import com.windy.dailynews.presenter.ThemeNewsContract;
import com.windy.dailynews.request.DataRequest;

import org.json.JSONObject;

/**
 * Created by dingfeng on 2016/11/22.
 */
public class ThemeNewsPresenter implements ThemeNewsContract.Presenter {

    private final ThemeNewsContract.View mView;

    public ThemeNewsPresenter(ThemeNewsContract.View view) {
        mView = view;
        mView.setPresenter(this);
    }

    @Override
    public void loadData(int id) {
        mView.setLoading();
        DataRequest.getInstance().getThemeNewsContent(id, new DataRequest.Callback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject data) {
                if (data != null) {
                    Gson gson = new Gson();
                    ThemeNews themeNews = gson.fromJson(data.toString(), ThemeNews.class);
                    mView.hideLoading();
                    mView.showCoverImage(themeNews.getImage());
                    mView.showNewsList(themeNews.getStories());
                } else {
                    mView.setError();
                }
            }

            @Override
            public void onError(String err) {
                mView.setError();
            }
        });
    }

    @Override
    public void start() {

    }

}
